﻿using SuperX.Common.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace SuperX.TimeManagerService
{
    public class BaseTimer
    {
        //定时器名称
        public string TimerName { get; set; }

        //定时器
        public Timer TimerCase = new Timer();

        /// <summary>
        /// 是否运行
        /// </summary>
        public bool IsRunning { get; set; }

        //异常委托
        public delegate void ExceptionDelegate(Exception ex);

        public ExceptionDelegate RegExceptionMethod;
        //public static event Action<BaseTimer, Exception> RegExceptionMethod;

        public BaseTimer(string timerName, double interval)
        {
            TimerName = timerName;
            TimerCase.Interval = interval;
            TimerCase.Enabled = false;
            TimerCase.Elapsed += Run;
        }

        private void Run(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                this.Run();
            }
            catch (Exception ex)
            {
                //日志记录,重启服务
                Logger.Warn(ex.Message);
            }
        }

        public virtual void Run()
        {
        }
    }
}